﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;

namespace inventory.Controls
{
    public class gridStatementInventory : common.controls.baseDataGridView 
    {
        public gridStatementInventory()
        {
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.AutoArrange();
            this.myOnFind += new OnCellEvent(FindData);
            this.myOnCellValueChanged += new OnCellEvent(ValueChanged);
        }

        public enum ColumnNames
        {
            itemCode, itemSubCode, techCode, itemName, unit, qty, deliveryLot, notes, actionBtn
        };
        private void UpdateItemList(string itemCode)
        {
            if (itemCode == null)
            {
                for (int idx = 0; idx < this.Rows.Count; idx++)
                {
                    if (this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value == null) continue;
                    DataAccess.DataCache.FindITEM(this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value.ToString());
                }
            }
            else DataAccess.DataCache.FindITEM(itemCode);
        }
        public override void Init(BindingSource dataSource)
        {
            base.Init(dataSource);
            data.applicationDS.StatementInventoryDataTable detailTbl = (data.applicationDS.StatementInventoryDataTable)dataSource.DataSource;

            DataGridViewCellStyle qtyCellType = new DataGridViewCellStyle();
            qtyCellType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            qtyCellType.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskQty).ToString();
            qtyCellType.NullValue = null;
            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 100;
            itemCode.Uppercase = true;

            // 
            // itemSubCode
            // 
            DataAccess.DataCache.LoadITEMSUBCODE();
            BindingSource itemSubCodeSource = new BindingSource();
            itemSubCodeSource.DataSource = DataAccess.DataCache.myMasterDS.itemSubCode;

            DataGridViewComboBoxColumn itemSubCode = new DataGridViewComboBoxColumn();
            itemSubCode.DataPropertyName = detailTbl.itemSubCodeColumn.ColumnName;
            itemSubCode.HeaderText = " +";
            itemSubCode.Name = ColumnNames.itemSubCode.ToString();
            itemSubCode.Width = 50;
            itemSubCode.DataSource = itemSubCodeSource;
            itemSubCode.DisplayMember = DataAccess.DataCache.myMasterDS.itemSubCode.codeColumn.ColumnName;
            itemSubCode.ValueMember = DataAccess.DataCache.myMasterDS.itemSubCode.codeColumn.ColumnName;
            itemSubCode.DefaultCellStyle.NullValue = " "; //Must be one space at least

            // 
            // techCode
            // 
            BindingSource itemSource = new BindingSource();
            itemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;

            DataGridViewComboBoxColumn techCode = new DataGridViewComboBoxColumn();
            techCode.DataSource = itemSource;
            techCode.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            techCode.DisplayMember = DataAccess.DataCache.myMasterDS.Item.techCodeColumn.ColumnName;
            techCode.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            techCode.HeaderText = "Mã kỹ thuật";
            techCode.Name = ColumnNames.techCode.ToString();
            techCode.ReadOnly = true;
            techCode.Width = 110;
            techCode.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            techCode.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            techCode.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // itemName
            // 
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = itemSource;
            itemName.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn gỉai";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            itemName.Width = 255;
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            // 
            // unit
            // 
            DataGridViewComboBoxColumn unit = new DataGridViewComboBoxColumn();
            unit.DataSource = itemSource;
            unit.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            unit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            unit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            unit.HeaderText = "ĐVT";
            unit.Name = ColumnNames.unit.ToString();
            unit.ReadOnly = true;
            unit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            unit.Width = 50;
            unit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            unit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // Qty
            // 
            DataGridViewTextBoxColumn qty = new DataGridViewTextBoxColumn();
            qty.DataPropertyName = detailTbl.qtyColumn.ColumnName;
            qty.HeaderText = "Lượng";
            qty.Name = ColumnNames.qty.ToString();
            qty.DefaultCellStyle = qtyCellType;
            qty.Width = 70;

            // 
            // deliveryLot
            // 
            common.controls.DataGridViewTextBoxColumnExt deliveryLot = new common.controls.DataGridViewTextBoxColumnExt();
            deliveryLot.DataPropertyName = detailTbl.deliveryLotColumn.ColumnName;
            deliveryLot.HeaderText = "Đợt";
            deliveryLot.Name = ColumnNames.deliveryLot.ToString();
            deliveryLot.Width = 35;
            deliveryLot.Uppercase = true;

            // 
            // Notes
            // 
            DataGridViewTextBoxColumn notes = new DataGridViewTextBoxColumn();
            notes.DataPropertyName = detailTbl.notesColumn.ColumnName; ;
            notes.HeaderText = "Ghi chú";
            notes.Name = ColumnNames.notes.ToString();
            notes.Width = 150;

            // 
            // actionBtn
            // 
            common.controls.gridViewImageColumn actionBtn = new common.controls.gridViewImageColumn();
            actionBtn.Name = ColumnNames.actionBtn.ToString();
            actionBtn.myImageType = common.controls.imageType.Edit;
            actionBtn.HeaderText = "";
            actionBtn.Width = 25;
            actionBtn.Visible = false;
            // 
            // this
            // 
            this.DataSource = this.myDataSource;

            this.AutoGenerateColumns = false;
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                    itemCode,itemSubCode,techCode,itemName,unit,qty,deliveryLot,notes,actionBtn});
            this.DisbaleReadOnlyColumns();
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
        }

        protected override void OnDefaultValuesNeeded(DataGridViewRowEventArgs e)
        {
            string deliveryLot = "";
            if (this.CurrentRow != null)
            {
                deliveryLot = this.CurrentRow.Cells[ColumnNames.deliveryLot.ToString()].EditedFormattedValue.ToString();
            }
            base.OnDefaultValuesNeeded(e);
            if (e == null) return;
            if (myDataSource.Current != null)
            {
                data.applicationDS.StatementInventoryRow statementInventoryRow = (data.applicationDS.StatementInventoryRow)((DataRowView)this.myDataSource.Current).Row;
                DataAccess.DataLibs.InitData(statementInventoryRow);
                if (this.Columns[ColumnNames.deliveryLot.ToString()].Visible)
                    statementInventoryRow.deliveryLot =  deliveryLot;
            }
        }
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        private void ValueChanged(object sender, common.controls.GridEventArgs e)
        {
            if (this.CurrentCell == null || !this.IsValidCell(e.RowIndex, e.ColumnIndex)) return;
            if (this.Columns[e.ColumnIndex].Name == ColumnNames.itemCode.ToString())
            {
                string itemCode = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (common.StringLibs.IsNullOrEmpty(itemCode) == false)
                {
                    bool HaveMarker = common.SysLibs.HaveFindMarker(ref itemCode);
                    if (HaveMarker == false)
                    {
                        string code  = application.AppLibs.GetItemCodeFromCodeId(itemCode);
                        if (code != null)
                        {
                            this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = code;
                            itemCode = code; 
                        }
                    }
                    data.masterDS.ItemRow itemRow = DataAccess.DataCache.FindITEM(itemCode);
                    if (itemRow == null) FindData(this, new common.controls.GridEventArgs(e.RowIndex, e.ColumnIndex));
                }
            }
        }
        private void FindData(object sender, common.controls.GridEventArgs arg)
        {
            string findCode = this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            //Code
            if (this.Columns[arg.ColumnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                application.dataFind.myItemFind.Find(findCode, true);
                data.masterDS.ItemRow itemRow = application.dataFind.myItemFind.selectedDataRow;
                if (itemRow != null)
                {
                    this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = itemRow.itemCode;
                    UpdateItemList(itemRow.itemCode);
                }
                else this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = DBNull.Value;
                this.myDataSource.ResetCurrentItem();
                this.myDataSource.EndEdit(); 
            }
        }
    }
}
